<?php
namespace app\admin\controller;

use app\admin\model\ArticleCat as ac;
use app\admin\model\ArticleRec as Rec;
use think\Db;
use my\Front;
use my\Page;
use my\Html;
use think\Input;

class ArticleRec extends Base
{   
    public function lists(){

        $rec        = new Rec();
        $rec_list   = $rec->lists();#推荐位
        
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    article_rec
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and title like ?";
                $param[] = "%{$keyword}%";
            }

            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
          
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by sort desc, id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){
                $list[$k]['checkbox']   =  "<input type='checkbox' name='id[]' tid='{$v['id']}'>";                
                $list[$k]['addtime']    =   $v['addtime'] ? date("Y-m-d H:i",$v['addtime']) : "";
                $list[$k]['sort']       =   "<input style='width:50px;' type='text' _url='".url('Articlerec/editSort')."' check='number' tid='{$v['id']}' old='{$v['sort']}' value='{$v['sort']}' onchange='com_edit(this)' />";
               
                $list[$k]['status']     =  com_format_status2(url('ajax/editStatus', 't=article_rec'),$v['id'],$v['status']);
               
                //操作按钮
                $list[$k]['action']    =   "<a class='ml-5' tid='{$v['id']}' _url='".url("Articlerec/edit","id={$v['id']}")."' msg='编辑文章' onclick='com_page(this)' href='javascript:;' title='编辑'><i class='Hui-iconfont'>&#xe6df;</i></a>";
                $list[$k]['action']   .=   "<a class='ml-5' tid='{$v['id']}' _url='".url('Articlerec/delete')."' msg='你确定要删除[{$v['title']}]吗?' onclick='com_del(this)' href='javascript:;' title='删除'><i class='Hui-iconfont'>&#xe6e2;</i></a>";
            }
            
			$com_body    = Html::buildTable($list, array("checkbox", "id",  "title", "sort", "status", "ext" , "addtime", "action"));
			$com_page    = Html::buildPage( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
            $config = [
                'data'   => $data,
                'suc'    => [
                    'js_action' => 'function.switchs',
                ],
            ];
            com_check($config);
			
        }
        
        //模板赋值
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['推荐位管理', '列表'],
        ];
        return view('', $data);
    }
    
    
    //编辑排序
    public function editSort(){
        if( is_post() ){
            $data   = [
                'id'    => input('id'),
                'sort'  => (int)input('val'),
            ];
            Db::name('article_rec')->update($data);
            
            $config = [
                'msg' => '编辑置顶成功！',
                'suc' => [
                    'js_action' => 'alert',
                ],
            ];
            Front::check($config);
        }
    }

    //编辑状态
    public function editStatus(){
        if( is_post() ){
            $data   = [
                'id'        => input('id'),
                'status'    => (int)input('val'),
            ];
            Db::name('article_rec')->update( $data );
    
            $config = [
                'msg' => '编辑状态成功！',
                'suc' => [
                    'js_action' => 'alert',
                ],
            ];
            Front::check( $config );
        }
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $data = input();  #post数据
            //验证
            $rule = [
                'title'      =>  'require',
                'sort'       =>  'require',
            ];
            $msg = [
                'title.require'      =>  '标题不能为空',
                'sort.require'       =>  '排序值不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['addtime'] = time();
            Db::name('article_rec')->insert($data);
            $config = [
                'msg' => '添加推荐位成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            front::check( $config );
        }    
        //模板赋值
        $data = [
            'status'    => c('type.status'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            unset($data['pic']);
            //验证
            $rule = [
                   'title'      =>  'require',
                   'sort'       =>  'require',
                ];
            $msg = [
                   'title.require'      =>  '标题不能为空',
                   'sort.require'       =>  '排序值不能为空',
                ];
            com_validate( $rule, $msg, $data, true );
            
            $data['edittime'] = time();
            $data['id'] = $id;
            Db::name('article_rec')->update($data);
            $config = [
                'msg' => '编辑推荐位成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            front::check( $config );
        }
        //模板赋值
        $info   = Db::name('article_rec')->find($id);
        $data = [
            'status'    => c('type.status'),
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    //删除
    public function delete(){
        if( is_post() ){
            $id = input('id');
            $rec  =  new Rec();
            if( $rec->checkDel($id) ){
                Db::name('article_rec')->delete( $id );
            }
            $config = [
                'err' => [
                    'js_action' => "alert",
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            Front::check( $config );
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            $ids = input('ids/a');
            $rec  =  new Rec();
            foreach($ids as $id){
                if( $rec->checkDel($id) ){
                    Db::name('article_rec')->delete( $id );
                }
            }
            
            $config = [
                'err' => [
                    'js_action' => 'alert',
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            Front::check($config);
        }
    }
}